<?php

namespace app\admin\service\statistics\traits;

use app\admin\service\statistics\filters\TimeFilter;

/**
 * Trait FilterDateTrait
 * @package app\admin\service\statistics\traits
 */
trait FilterDateTrait
{

    /**
     * 实现时间筛选
     * @param \think\db\Query $query
     * @param TimeFilter $filter
     * @return void
     */
    protected function filterByTimeFilter($query, TimeFilter $filter): void
    {

        $startTime = $filter->getStartTime();
        $endTime = $filter->getEndTime();

        if (!is_null($startTime)) {
            $startDate = date('Y-m-d', $startTime);
             $query->where('date','>=', $startDate);
        }

        if (!is_null($endTime)) {
            $endDate = date('Y-m-d', $endTime);
            $query->where('date','<=', $endDate);
        }

    }
}